﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using CCH.DAO;
using CCH.Entity;

public partial class lib_FileUpload : System.Web.UI.UserControl
{
    /// <summary>
    /// 照片路徑含檔名
    /// </summary>
    public string doc_PICpath { get; set; }
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.Image1.Visible = false;

            if (!string.IsNullOrEmpty(this.doc_PICpath))
            {
                this.Image1.Visible = true;
                this.Image1.ImageUrl = ResolveClientUrl("~/lib/peoplePIC.ashx?doc_picpath=" + this.doc_PICpath);
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        this.div_pic.InnerHtml = "";
        this.Image1.ImageUrl = "";
        this.lab_filename.Text = "";
        this.lab_path.Text = "";

        this.SavePIC();
        
    }

    private void SavePIC()
    {
        if (this.FileUpload1.HasFile)
        {
            //存檔目錄
            string root = System.Configuration.ConfigurationManager.AppSettings["peoplePIC_PATH"];

            //副檔名
            string exi = this.FileUpload1.FileName.Split('.')[this.FileUpload1.FileName.Split('.').Length - 1].ToLower();

            if (exi.Equals("jpeg") || exi.Equals("jpg"))
            {
                //檔名
                string filename = Guid.NewGuid().ToString("N") + "." + exi;

                //上傳
                this.FileUpload1.SaveAs(root + filename);

                this.lab_filename.Text = filename;

                this.lab_path.Text = root;

                this.Image1.Visible = true;
                this.Image1.ImageUrl = ResolveClientUrl("~/lib/peoplePIC.ashx?doc_picpath=" + root + filename);

            }
            else
            {
                this.div_pic.InnerHtml = "請上傳jpeg,jpg等圖檔";
            }

        }
        
    }

    public string Get_FileName
    {
        get
        {
            try
            {
                return this.lab_filename.Text;
            }
            catch
            {
                return "";
            }
        }
    }

    public string Get_FilePath
    {
        get
        {
            try
            {
                return this.lab_path.Text;
            }
            catch
            {
                return "";
            }
        }
    }
    
}
